package com.raven.play.chat;

import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.Behaviors;
import com.raven.play.chat.msg.*;

public class Session {
    public static Behavior<SessionCommand> create(ActorRef<RoomCommand> room, String screenName, ActorRef<SessionEvent> client) {
        return Behaviors.receive(SessionCommand.class).
                onMessage(PostMessage.class, post -> onPostMessage(room, screenName, post))
                .onMessage(NotifyClient.class, notification -> onNotifyClient(client, notification)).build();
    }

    private static Behavior<SessionCommand> onPostMessage(ActorRef<RoomCommand> room, String screenName, PostMessage post) {
        // from client, publish to others via the room
        room.tell(new PublishSessionMessage(screenName, post.message()));
        return Behaviors.same();
    }

    private static Behavior<SessionCommand> onNotifyClient(ActorRef<SessionEvent> client, NotifyClient notification) {
        // published from the room
        client.tell(notification.message());
        return Behaviors.same();
    }
}